/**
 * @file DHT11.c    
 * @author your name (you@domain.com)
 * @brief DHT11驱动源文件
 * @version 0.1
 * @date 2024-03-28
 * 
 * @copyright Copyright (c) 2024
 * 
 */
#include "DHT11.h"

float Temp_data;
float Humi_data;

/**
 * @brief 引脚输出模式
 * 
 */
void DHT11_Init_Send(void)
{
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
    
    GPIO_InitTypeDef GPIO_InitStructure;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOB,&GPIO_InitStructure);
}


/**
 * @brief 引脚输入模式
 * 
 */
void DHT11_Init_Read(void)
{
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
    
    GPIO_InitTypeDef GPIO_InitStructure;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOB,&GPIO_InitStructure);
}


/**
 * @brief 读取一个字节
 * 
 * @return uint8_t 返回读取的字节
 */
uint8_t DHT11_RecBit(void)
{
    uint8_t Data = 0;
    uint8_t i = 0;
    for ( i = 0; i < 8; i++)
    {
        Data <<= 1;
        while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_13) == 1);
        while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_13) == 0);
        Delay_us(40);
        if (GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_13) == 1)
        {
            Data |= 1;

        }
    }
    
    return Data;
    
}

/**
 * @brief 开始读取片段
 * 
 */
void DHT11_Start(void)
{
    DHT11_Init_Send();
    GPIO_WriteBit(GPIOB,GPIO_Pin_13,Bit_SET);
    Delay_us(30);
    GPIO_WriteBit(GPIOB,GPIO_Pin_13,Bit_RESET);
    Delay_ms(20);
    GPIO_WriteBit(GPIOB,GPIO_Pin_13,Bit_SET);
    Delay_us(30);
    DHT11_Init_Read();
}

/**
 * @brief 停止读取片段
 * 
 */
void DHT11_Stop(void)
{
    DHT11_Init_Send();
    GPIO_WriteBit(GPIOB,GPIO_Pin_13,Bit_RESET);
    Delay_us(55);
    GPIO_WriteBit(GPIOB,GPIO_Pin_13,Bit_SET);
}



/**
 * @brief DHT11读取温度湿度数据
 * 
 */
void DHT11_RecData(float* Tem,float* Hum)
{
    uint8_t R_H,R_L,T_H,T_L,CHECK;
    float RH,RL,TH,TL;
    DHT11_Start();
    Delay_us(20);
    if (GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_13) == 0)
    {
        while (GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_13) == 0);        
        R_H = DHT11_RecBit();
        R_L = DHT11_RecBit();
        T_H = DHT11_RecBit();
        T_L = DHT11_RecBit();
        CHECK = DHT11_RecBit();
        DHT11_Stop();
        if (CHECK == R_H+R_L+T_H+T_L)
        {
            RH = (float)R_H;
            RL = (float)R_L;
            RL /= 100;
            TH = (float)T_H;
            TL = (float)T_L;
            TL /= 100;
            Temp_data = TH + TL;
            Humi_data = RH + RL;
            *Tem = Temp_data;
            *Hum = Humi_data;
        }
        
    }
}


/**
 * @brief 调用此函数得到温度值
 * 
 * @return float 返回温度值
 */
float DHT11_GetTempData(void)
{
    return Temp_data;
}

/**
 * @brief 调用此函数得到湿度值
 * 
 * @return float 返回湿度值
 */
float DHT11_GetHumiData(void)
{
    return Humi_data;
}




/**
 * @brief DHT11初始化函数
 * 
 */
void DHT11_Init(void)
{
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);
	TIM_InternalClockConfig(TIM1);
    TIM_TimeBaseInitTypeDef TIN_TimeBaseInitStructure;
    TIN_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
    TIN_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIN_TimeBaseInitStructure.TIM_Period = 9999;
    TIN_TimeBaseInitStructure.TIM_Prescaler = 7199;
    TIN_TimeBaseInitStructure.TIM_RepetitionCounter = 0;
    TIM_TimeBaseInit(TIM1,&TIN_TimeBaseInitStructure);

    TIM_ClearFlag(TIM1,TIM_FLAG_Update);
    TIM_ITConfig(TIM1,TIM_IT_Update,ENABLE);

    NVIC_InitTypeDef NVIC_InitStructure;
    NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
    NVIC_Init(&NVIC_InitStructure);

    TIM_Cmd(TIM1,ENABLE);
}

/**
 * @brief 中断函数，每次中断刷新一次数据
 * 
 */
void TIM1_UP_IRQHandler(void)
{
    if (TIM_GetITStatus(TIM1,TIM_IT_Update) == SET)
    {
        // if (GPIO_ReadOutputDataBit(GPIOC,GPIO_Pin_13) == 1)
        // {
        //     GPIO_WriteBit(GPIOC,GPIO_Pin_13,Bit_RESET);
        // }
        // else
        // {
        //     GPIO_WriteBit(GPIOC,GPIO_Pin_13,Bit_SET);
        // }
        DHT11_RecData(&Temp_data,&Humi_data);
        TIM_ClearITPendingBit(TIM1,TIM_IT_Update);
    }
    
}


